package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Department;
import cn.wolfcode.mapper.DepartmentMapper;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class DepartmentImplService implements IDepartmentService {
   @Autowired
    private DepartmentMapper departmentMapper;

    public void save (Department department) {
        departmentMapper.insert(department);
    }

    public void updateById (Department department) {
        departmentMapper.updateByPrimaryKey(department);
    }

    public void deleteById (Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }

    public Department selectById (Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    public List<Department> listAll () {
        return departmentMapper.selectAll();
    }

    @Override
    public PageInfo<Department> query (QueryObject qo) {
        /*int totalCount = departmentMapper.queryForCount(qo);
        if (totalCount == 0 ){
            return new pageInfo<>(qo.getCurrentPage(),qo.getPageSize());
        }*/

        //告诉pagehelper我们的分页参数(只对下一句sql进行自动分页)
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Department> result = departmentMapper.queryForList(qo);
        return new PageInfo<>(result);
    }
}
